package com.yihen.service.impl;

import com.yihen.dto.Result;
import com.yihen.mappers.DoctorMapper;
import com.yihen.pojo.Doctor;
import com.yihen.pojo.Patient;
import com.yihen.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import util.Constants;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {
    @Autowired
    private DoctorMapper doctorMapper;

    @Override
    public Result getAllDoctor() {
        // 查询所有医生信息
        List<Doctor> doctors = doctorMapper.queryAll();
        // 装载到统一类型中并返回
        return new Result(doctors);
    }

    @Override
    public Result addDoctor(Doctor doctor) {
        // 添加医生对象
        try {
            int tag = doctorMapper.insert(doctor);
        } catch (Exception e) {
            // 添加失败，返回失败信息
            return new Result(Constants.ADDFAILED);
        }
        // 添加成功，返回成功信息
        return new Result(null, true, Constants.ADDSUCCESS);
    }

    @Override
    public Result updateDoctorById(Doctor doctor) {
        // 修改医生信息
        int tag = doctorMapper.updateById(doctor);
        // 修改成功，返回成功信息
        return new Result(null, true, Constants.UPDATESUCCESS);
    }

    @Override
    public Result deleteDoctorById(long id) {
        // 删除医生信息
        // TODO 完善
        int tag = doctorMapper.deleteById(id);
        if (tag <= 0) {
            return new Result(Constants.DELETEFAILED);
        }
        return new Result(null, true, Constants.DELETESUCCESS);

    }



    @Override
    public Result getDoctorByCondition(Doctor doctor) {
        List<Doctor> doctors = doctorMapper.queryDoctorByCondition(doctor);
        return new Result(doctors);
    }

    @Override
    public Result deleteDoctorsByIds(ArrayList<Long> ids) {
        // TODO 完善
        int tag = doctorMapper.deleteByIds(ids);
        if (tag <= 0) {
            return new Result(Constants.DELETEFAILED);
        }
        return new Result(null, true, Constants.DELETESUCCESS);
    }

    @Override
    public Result getDoctorById(long id) {
        Doctor doctor = doctorMapper.queryDoctorById(id);
        return new Result(doctor);
    }
}
